package com.gthncz.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.gthncz.mbg.pojo.AuthRoleUser;
import com.gthncz.mbg.pojo.TbAuth;
import com.gthncz.mbg.pojo.TbRole;

public class UserDetailsImpl implements UserDetails{
	
	private static final long serialVersionUID = 1L;
	private AuthRoleUser user;
	
	public UserDetailsImpl(AuthRoleUser user) {
		super();
		this.user = user;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		List<TbAuth> auths = user.getAuths();
		if(auths != null) {
			for (TbAuth tbAuth : auths) {
				grantedAuthorities.add(new GrantedAuthorityImpl(tbAuth.getAuth()));
			}
		}
		List<TbRole> roles = user.getRoles();
		if(roles != null) {
			for (TbRole tbRole : roles) {
				grantedAuthorities.add(new GrantedAuthorityImpl(tbRole.getRole()));
			}
		}
		return grantedAuthorities;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
